package com.swjd.jingle.fragment;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.viewpager.widget.ViewPager;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.tabs.TabLayout;
import com.swjd.jingle.R;
import com.swjd.jingle.activity.MainActivity;
import com.xuexiang.xui.adapter.FragmentAdapter;

import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.OnClick;

import static com.google.android.material.tabs.TabLayout.MODE_FIXED;

public class MeGoodsFragment extends BaseFragment implements View.OnClickListener {

    @BindView(R.id.tab_layout)
    TabLayout tabLayout;
    @BindView(R.id.view_pager)
    ViewPager viewPager;
    @BindView(R.id.fab)
    FloatingActionButton fab;
    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_me_goods;
    }

    String[] titles = new String[]{"我卖出的", "我买到的", "我收藏的"};

    @Override
    public void initialize() {
        tvTitle.setText("闲置");
    }

    @Override
    public void initViews() {
        FragmentAdapter<BaseFragment> adapter = new FragmentAdapter<>(getChildFragmentManager());
        tabLayout.setTabMode(MODE_FIXED);
        for (String title : titles) {
            tabLayout.addTab(tabLayout.newTab().setText(title));
            adapter.addFragment(new InfoFragment(), title);
        }
        viewPager.setOffscreenPageLimit(titles.length - 1);
        viewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewPager);
    }

    @Override
    public void initData() {
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position == 0) {
                    fab.show();
                } else {
                    fab.hide();
                }
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });

    }

    @Override
    @OnClick({R.id.img_back, R.id.fab})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                new Timer().schedule(new TimerTask() {
                    @Override
                    public void run() {
                        fragmentManager.popBackStack();
                        ((MainActivity) activity).openSide();
                    }
                }, 500);
                break;
            case R.id.fab:
                break;
        }
    }


}
